package com.example.demo.listeners;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.retry.backoff.Sleeper;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lishuo
 * Date: 2024-02-04
 * Time: 16:18
 */
@Component
@Slf4j
public class MqListener {
    // 默认交换机消费示例
//    @RabbitListener(queues = "example.queue")
//    public void listenExampleQueue(String msg) {
//        log.info("spring 消费者接收到消息：【"+ msg +"】");
//        // ...业务逻辑
//        log.info("消息处理完毕!");
//    }

    // work 模式示例-默认交换机（多个消费者绑定同一个队列）
    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1：收到 work.queue 的消息："+ msg);
        Thread.sleep(20);
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2：收到 work.queue 的消息："+ msg);
        Thread.sleep(200);
    }

    // Fanout交换机示例
//    @RabbitListener(queues = "fanout.queue1")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue1"),
            exchange = @Exchange(name = "forum.fanout",type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueue1(String msg) {
        System.out.println("消费者1：收到的消息："+ msg);
    }

//    @RabbitListener(queues = "fanout.queue2")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue2"),
            exchange = @Exchange(name = "forum.fanout",type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueue2(String msg) {
        System.out.println("消费者2：收到的消息："+ msg);
    }

    // Direct交换机示例
//    @RabbitListener(queues = "direct.queue1")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "forum.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void listenDirectQueue1(String msg) {
        System.out.println("消费者1：收到的消息："+ msg);
    }

//    @RabbitListener(queues = "direct.queue2")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "forum.direct",type = ExchangeTypes.DIRECT),
            key = {"red"}
    ))
    public void listenDirectQueue2(String msg) {
        System.out.println("消费者2：收到的消息："+ msg);
    }

    // Topic交换机示例
//    @RabbitListener(queues = "topic.queue1")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "forum.topic",type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void listenTopicQueue1(String msg) {
        System.out.println("消费者1：收到的消息："+ msg);
    }

//    @RabbitListener(queues = "topic.queue2")
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "forum.topic",type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void listenTopicQueue2(String msg) {
        System.out.println("消费者2：收到的消息："+ msg);
    }

    // 处理对象消息示例
    @RabbitListener(queues = "example.queue")
    public void listenObjectQueue(Map<String,Object> msg) {
        System.out.println("消费者：收到的消息："+ msg);
    }

    // 异常实例
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg) {
        System.out.println("消费者收到消息："+msg);
        throw new RuntimeException("故意的");
    }

    // 延迟消息（插件方式）
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue",durable = "true"),
            exchange = @Exchange(name = "delay.direct",delayed = "true",type = ExchangeTypes.DIRECT),
            key = "delay"
    ))
    public void listenDelayMessage(String msg) {
        log.info("接收到delay.queue的延迟消息 {}"+msg);
    }

}
